/*
编写 : Hexh
时间 : 2012-11-29 15:12:53
*/
using System.Collections.Generic;
using HMIS.FrameWork.DomainBase;
using HMIS.Infrastructure.Core.Repositories;
using HMIS.Infrastructure.Core.Entities;
using System;
using Iesi.Collections.Generic;

namespace HMIS.Infrastructure.Facade.Impl
{
	/// <summary>
	///	 the impl of IInfraApproveHisFacade
	/// </summary>
	public class InfraApproveHisFacadeImpl : IInfraApproveHisFacade
	{
		private readonly IInfraApproveHisStepRepository InfraApproveHisStepRepository;
		private readonly IInfraApproveHisRepository InfraApproveHisRepository;
		private readonly IInfraApproveRepository InfraApproveRepository;

		public InfraApproveHisFacadeImpl(IInfraApproveHisRepository InfraApproveHisRepository,
			IInfraApproveRepository InfraApproveRepository,
			IInfraApproveHisStepRepository InfraApproveHisStepRepository)
		{
			this.InfraApproveHisRepository = InfraApproveHisRepository;
			this.InfraApproveRepository = InfraApproveRepository;
			this.InfraApproveHisStepRepository = InfraApproveHisStepRepository;
		}

		#region IInfraApproveHis Entity Save,Get,Update,Delete

		public InfraApproveHis Get(string id)
		{
			return InfraApproveHisRepository.Get(id);
		}

		public InfraApproveHis Load(string id)
		{
			return InfraApproveHisRepository.Load(id);
		}

		public void SaveOrUpdate(InfraApproveHis entity)
		{
			this.InfraApproveHisRepository.SaveOrUpdate(entity);
		}

		public void Delete(string id)
		{
			InfraApproveHisRepository.Delete(id);
		}

		public void PhysicsDelete(string id)
		{
			InfraApproveHisRepository.PhysicsDelete(id);
		}

		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return InfraApproveHisRepository.IsFieldExist(fieldName, fieldValue, id);
		}

		public bool IsFieldExistWhere(string fieldName, string fieldValue, string id, string where)
		{
			return InfraApproveHisRepository.IsFieldExistWhere(fieldName, fieldValue, id, where);
		}

		/// <summary>
		/// 提交审批，初始化审批流程
		/// </summary>
		/// <param name="itemId"></param>
		/// <param name="approveType"></param>
		/// <param name="deptId"></param>
		/// <returns></returns>
		public InfraApproveHis CreateApproveHis(string itemId, string approveSms, string approveType, string deptId)
		{
			InfraApprove approveInfo = InfraApproveRepository.GetByDeptAndType(deptId, approveType);
			if (approveInfo == null)
				throw new Exception("未定义审批流程!请联系系统管理员。");

			InfraApproveHis approveHis = new InfraApproveHis
			{
				ApproveId = approveInfo.Id,
				ApproveItemId = itemId,
				SmsText = approveSms,
				ApproveState = 0,
				ApprSteps = new HashedSet<InfraApproveHisStep>()
			};

			foreach (InfraApproveStep step in approveInfo.InfraApproveSteps)
			{
				InfraApproveHisStep apprStep = new InfraApproveHisStep
				{
					ApproveHisId = approveHis.Id,
					UserId = step.UserId,
					ApproveStep = step.ApproveStep,
					Scale = step.Scale,
					AprCode = Guid.NewGuid().ToString().Substring(0, 8)
				};
				approveHis.ApprSteps.Add(apprStep);

			}
			InfraApproveHisRepository.SaveOrUpdate(approveHis);
			return approveHis;
		}

		#endregion

		#region Query

		public IList<InfraApproveHis> GetAll()
		{
			return InfraApproveHisRepository.GetAll();
		}

		public IList<InfraApproveHis> GetPlist(string query, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			return InfraApproveHisRepository.GetPlist(query, start, limit, sort, dir, filters, out total);
		}

		public InfraApproveHisStep GetApproveHisStepById(string id)
		{
			return this.InfraApproveHisStepRepository.Get(id);
		}

		public IList<InfraApproveHisStep> GetSteplist(string approveHisId, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			return this.InfraApproveHisStepRepository.GetPlist(approveHisId, start, limit, sort, dir, filters, out total);
		}

		#endregion
	}
}
